package com.test.homework4;

public class Homework {
    public static void main(String[] args) {
        Share share = new Share();
        share.setJ(0);


        FirstAdd fa = new FirstAdd(share);
        SecondAdd sa = new SecondAdd(share);
        FirstSub fs = new FirstSub(share);
        SecondSub ss = new SecondSub(share);

        fa.start();
        sa.start();
        fs.start();
        ss.start();

        System.out.println(share.getJ());

    }
}

class FirstAdd extends Thread{
    private Share share;

    @Override
    public void run() {
        synchronized (share){
            share.setJ(share.getJ()+1);
            System.out.println(Thread.currentThread().getName()+":"+share.getJ());
        }
    }

    public FirstAdd(Share share) {
        this.share = share;
    }

    public Share getShare() {
        return share;
    }

    public void setShare(Share share) {
        this.share = share;
    }
}

class SecondAdd extends Thread{
    private Share share;

    @Override
    public void run() {
        synchronized (share){
            share.setJ(share.getJ()+1);
            System.out.println(Thread.currentThread().getName()+":"+share.getJ());
        }
    }

    public SecondAdd(Share share) {
        this.share = share;
    }

    public Share getShare() {
        return share;
    }

    public void setShare(Share share) {
        this.share = share;
    }
}

class FirstSub extends Thread{
    private Share share;

    @Override
    public void run() {
        synchronized (share){
            share.setJ(share.getJ()-1);
            System.out.println(Thread.currentThread().getName()+":"+share.getJ());
        }
    }

    public FirstSub(Share share) {
        this.share = share;
    }

    public Share getShare() {
        return share;
    }

    public void setShare(Share share) {
        this.share = share;
    }
}

class SecondSub extends Thread{
    private Share share;

    @Override
    public void run() {
        synchronized (share){
            share.setJ(share.getJ()-1);
            System.out.println(Thread.currentThread().getName()+":"+share.getJ());
        }
    }

    public SecondSub(Share share) {
        this.share = share;
    }

    public Share getShare() {
        return share;
    }

    public void setShare(Share share) {
        this.share = share;
    }
}

class Share{
    private int j;

    public int getJ() {
        return j;
    }

    public void setJ(int j) {
        this.j = j;
    }
}
